package com.ssau.expert.gui.common;

import com.ssau.expert.entity.Ship;
import com.ssau.expert.gui.app.Locator;

import javax.swing.*;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import java.awt.*;
import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Author: 345
 * Date: 24.09.13 7:23
 */
public class MyControls {

    public static final Dimension BUTTON_SIZE = new Dimension(190, 25);
    public static final Dimension X_BUTTON_SIZE = new Dimension(120, 25);
    private static final Font BUTTON_FONT = new Font("Times New Roman", Font.BOLD, 10);
    private static final Font LABEL_FONT = new Font("Times New Roman", Font.BOLD, 15);
    public static final int MARGIN = 5;

    private static Object[] SHIP_PARAMETERS = {"Название", "Профиль", "Водоизмещение",
            "Автономность1", "CF A1",
            "Экипаж1", "CF Э1",
            "Автономность2", "CF A2",
            "Экипаж2", "CF Э2"
    };

    public static JButton createButton(String text, int x, int y) {
        JButton myButton = new JButton(text);
        myButton.setVisible(true);
        myButton.setSize(BUTTON_SIZE);
        myButton.setLocation(x, y);
        myButton.setFont(LABEL_FONT);
        return myButton;
    }

    public static JLabel createLabel(String text, int x, int y) {
        JLabel myLabel = new JLabel(text);
        myLabel.setSize(BUTTON_SIZE);
        myLabel.setFont(LABEL_FONT);
        myLabel.setLocation(x, y);
        return myLabel;
    }

    public static JTextArea CreateJTextArea(int x, int y, int width, int height, boolean readOnly) {
        JTextArea myJTextArea = new JTextArea();
        myJTextArea.setFont(LABEL_FONT);
        myJTextArea.setSize(width, height);
        myJTextArea.setLocation(x, y);
        myJTextArea.setFocusable(!readOnly);

        return myJTextArea;
    }

    public static JTextField CreateJTextField(int x, int y, boolean readOnly) {
        JTextField myJTextField = new JTextField();
        myJTextField.setFont(LABEL_FONT);
        myJTextField.setSize(BUTTON_SIZE);
        myJTextField.setLocation(x, y);
        myJTextField.setFocusable(!readOnly);
        return myJTextField;
    }

    public static JPanel createJPanel(Frame frame, int x, int y, int width, int height, String text) {
        JPanel myJPanel = new JPanel();
        myJPanel.setLayout(null);
        myJPanel.setSize(width, height);
        myJPanel.setLocation(x, y);
        myJPanel.setFont(LABEL_FONT);
        myJPanel.setBorder(BorderFactory.createTitledBorder(text));
        frame.add(myJPanel);
        return myJPanel;
    }

    public static File OpenFileDialog(JFrame frame) {
        JFileChooser myJFileChooser = new JFileChooser();
        myJFileChooser.showDialog(frame, "Открыть файл");

        return myJFileChooser.getSelectedFile();
    }

    public static DefaultListModel CreateJList(JFrame frame, int x, int y, int width, int height) {
        DefaultListModel<String> listModel = new DefaultListModel<>();
        JList<String> list = new JList<>(listModel);
        list.setLocation(x, y);
        list.setSize(width, height);
        list.setFont(LABEL_FONT);
        list.setLayoutOrientation(JList.VERTICAL);
        frame.add(list);

        return listModel;
    }

    public static DefaultListModel CreateJList(JPanel jPanel, int x, int y, int width, int height) {
        DefaultListModel<String> listModel = new DefaultListModel<>();
        JList<String> list = new JList<>(listModel);
        list.setLocation(x, y);
        list.setSize(width, height);
        list.setFont(LABEL_FONT);
        list.setLayoutOrientation(JList.VERTICAL);

        //JScrollPane jScrollPane = new JScrollPane(list);
        //jPanel.add(jScrollPane, BorderLayout.CENTER);
        jPanel.add(list);

        return listModel;
    }

    public static JPanel createPanel(int x, int y, int width, int height, String text) {
        JPanel myJPanel = new JPanel();
        myJPanel.setLayout(null);
        myJPanel.setSize(width, height);
        myJPanel.setLocation(x, y);
        myJPanel.setFont(LABEL_FONT);
        myJPanel.setBorder(BorderFactory.createTitledBorder(text));
        return myJPanel;
    }

    public static JTable createExpertTable(int x, int y, int width, int height) {
        JTable table;
        List<Ship> shipList = Locator.getDatabase().getShips();
        String[][] data = new String[shipList.size()][11];
        for (int i = 0; i < shipList.size(); i++) {
            data[i][0] = shipList.get(i).getShipName().getLocalName();
            data[i][1] = shipList.get(i).getShipProfile().getName();
            data[i][2] = String.valueOf(shipList.get(i).getWaterVolume());

            data[i][3] = String.valueOf(shipList.get(i).getMaxDistance1());
            data[i][4] = String.valueOf(shipList.get(i).getCfMaxDistance1());

            data[i][5] = String.valueOf(shipList.get(i).getMaxStaff1());
            data[i][6] = String.valueOf(shipList.get(i).getCfMaxStaff1());

            data[i][7] = String.valueOf(shipList.get(i).getMaxDistance1());
            data[i][8] = String.valueOf(shipList.get(i).getCfMaxDistance1());

            data[i][9] = String.valueOf(shipList.get(i).getMaxStaff1());
            data[i][10] = String.valueOf(shipList.get(i).getCfMaxStaff1());
        }
        table = new JTable(data, SHIP_PARAMETERS);
        table.setLocation(x, y);
        table.setSize(width, height);
        return table;
    }

    public static JTable getExpertTable(int x, int y, int width, int height) {
        final List<Ship> shipList = Locator.getDatabase().getShips();
        TableModel tableModel = new TableModel() {

            Set<TableModelListener> listeners = new HashSet<TableModelListener>();

            @Override
            public int getRowCount() {
                return shipList.size();
            }

            @Override
            public int getColumnCount() {
                return SHIP_PARAMETERS.length;
            }

            @Override
            public String getColumnName(int columnIndex) {
                return SHIP_PARAMETERS[columnIndex].toString();
            }

            @Override
            public Class<?> getColumnClass(int columnIndex) {
                return String.class;
            }

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return false;
            }

            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {
                Ship ship = shipList.get(rowIndex);
                switch (columnIndex) {
                    case 0:
                        return ship.getShipName().getLocalName();
                    case 1:
                        return ship.getShipProfile().getName();
                    case 2:
                        return ship.getWaterVolume();
                    case 3:
                        return ship.getMaxDistance1();
                    case 4:
                        return ship.getCfMaxDistance1();
                    case 5:
                        return ship.getMaxStaff1();
                    case 6:
                        return ship.getCfMaxStaff1();
                    case 7:
                        return ship.getMaxDistance2();
                    case 8:
                        return ship.getCfMaxDistance2();
                    case 9:
                        return ship.getMaxStaff2();
                    case 10:
                        return ship.getCfMaxStaff2();
                    default:
                        return "";
                }
            }

            @Override
            public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

            @Override
            public void addTableModelListener(TableModelListener l) {
                listeners.add(l);
            }

            @Override
            public void removeTableModelListener(TableModelListener l) {
                listeners.remove(l);
            }
        };

        JTable jTable = new JTable(tableModel);


        jTable.setLocation(x, y);
        jTable.setSize(width, height);

        jTable.setAutoResizeMode(jTable.AUTO_RESIZE_ALL_COLUMNS);
        jTable.getColumnModel().getColumn(0).setPreferredWidth(130);
        jTable.getColumnModel().getColumn(1).setPreferredWidth(100);

        return jTable;
    }

    public static JTextField createJTextField(int x, int y, int width, int height, boolean editable) {
        JTextField tf = new JTextField();
        tf.setLocation(x, y);
        tf.setSize(width, height);
        tf.setEditable(editable);
        return tf;
    }

    public static JTextField createTextField(int x, int y) {
        return createTextField(x, y, BUTTON_SIZE.width, BUTTON_SIZE.height);
    }

    public static JTextField createTextField(int x, int y, int width, int height) {
        JTextField textField = new JTextField();
        textField.setSize(new Dimension(width, height));
        textField.setFont(LABEL_FONT);
        textField.setLocation(x, y);
        return textField;
    }

    public static JTextField createTextField(int number, int x0, int y) {
        JTextField textField = new JTextField();
        textField.setSize(new Dimension(X_BUTTON_SIZE.width / 2, X_BUTTON_SIZE.height));
        textField.setFont(LABEL_FONT);
        textField.setLocation(x0 + number * (X_BUTTON_SIZE.width / 2), y);
        return textField;
    }
}
